package com.bbb.controller;

import com.bbb.common.Code;
import com.bbb.common.Result;
import com.bbb.pojo.User;
import com.bbb.service.UserService;
import com.bbb.common.util.JwtAuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    // 用于存储失效的 JWT 令牌的黑名单
    private static final Set<String> TOKEN_BLACKLIST = new HashSet<>();

    @GetMapping("/getAll")
    public List<User> getAll() {
        List<User> list = userService.getAll();
        return list;
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User login = userService.login(user);
        System.out.println(login);
        if (login != null) {
            // 生成 JWT
            String token = JwtAuthUtil.generateToken(login);
            // 将 JWT 放入响应数据中
            Result result = new Result(Code.GET_OK, "登录成功", login);
            result.setToken(token);
            return result;
        } else {
            return new Result(Code.GET_ERR, "登录失败");
        }
    }

    @PostMapping("/update")
    public Result update(
            @RequestHeader(value = "Authorization", required = false) String authorizationHeader,
            @RequestParam("id") int id,
            @RequestParam("name") String name,
            @RequestParam("password") String password,
            @RequestParam("age") Integer age,
            @RequestParam("tel") String tel,
            @RequestParam(value = "avatar", required = false) String avatar
    ) {
        if (authorizationHeader == null) {
            return new Result(Code.GET_ERR, "请求头中缺少 Authorization 字段");
        }
        // 从请求头中提取 JWT
        String token = authorizationHeader.replace("Bearer ", "");
        // 验证 JWT
        if (!JwtAuthUtil.validateToken(token)) {
            return new Result(Code.GET_ERR, "无效的 token，无法进行更新操作");
        }

        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPassword(password);
        user.setAge(age);
        user.setTel(tel);
        user.setAvatar(avatar);

        int update = userService.update(user);
        if (update == 1) {
            return new Result(Code.GET_OK, "修改成功", update);
        } else {
            return new Result(Code.GET_ERR, "修改失败");
        }
    }


    // 处理文件上传的方法，你需要根据实际情况实现具体逻辑
    private String handleAvatarUpload(MultipartFile avatar) throws IOException {
        // 这里只是示例，你需要实现具体的文件保存逻辑
        // 例如将文件保存到指定目录并返回文件路径
        return "path/to/save/avatar.jpg";
    }

    @PostMapping("/logout")
    public Result logout(@RequestHeader("Authorization") String authorizationHeader) {
        // 从请求头中提取 JWT
        String token = authorizationHeader.replace("Bearer ", "");
        // 验证 JWT
        if (!JwtAuthUtil.validateToken(token)) {
            return new Result(Code.GET_ERR, "无效的 token，无法进行退出操作");
        }
        // 将令牌添加到黑名单
        TOKEN_BLACKLIST.add(token);
        return new Result(Code.GET_OK, "退出成功");
    }
}